package com.compass.mobile.framework.ifly;

import static com.compass.mobile.framework.constant.AppConstantKt.IFLYTEK_APPID;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.compass.mobile.framework.helper.CompassAppHelper;
import com.iflytek.xiri3.open.XiriOPManager;
import com.iflytek.xiri3.open.callback.XiriOpCallback;
import com.iflytek.xiri3.open.constant.ServiceConstant;
import com.iflytek.xiri3.open.features.semantic.ISemantic;
import com.iflytek.xiri3.open.features.text.IText;
import com.iflytek.xiri3.open.features.ui.IFeedback;
import com.netease.nim.highavailable.LogUtils;

/**
 * 讯飞工具类
 */
public class IFlyHelper {

    private final static String TAG = "IFlyHelper";
    /**
     * 是否初始化
     */
    public static boolean isInit = false;

//    public static void initIFly(Context context){
//        initIFly(context,null);
//    }

    public static void initIFly(Context context, IFlyOptCallback callback){
        XiriOPManager.getInstance().init(context, IFLYTEK_APPID, i -> {
            String text = "";
            if (i == XiriOpCallback.AUTH_FAILED) {
                text = "授权失败";
                if(callback!=null){
                    callback.onFail(XiriOpCallback.STATUS_DISCONNECT,text);
                }
            } else if (i == XiriOpCallback.STATUS_CONNECT) {
                text = "已连接";
            } else if (i == XiriOpCallback.STATUS_DISCONNECT) {
                text = "已断开";
                if(callback!=null){
                    callback.onFail(XiriOpCallback.STATUS_DISCONNECT,text);
                }
            } else if (i == XiriOpCallback.INIT_FAILED) {
                text = "初始化失败";
                isInit = false;
                if(callback!=null){
                    callback.onFail(XiriOpCallback.INIT_FAILED,text);
                }
            } else if (i == XiriOpCallback.AUTH_ING) {
                text = "授权中...";
            } else if (i == XiriOpCallback.AUTH_SUCCESS) {
                text = "授权成功";
                isInit = true;
                if(callback!=null){
                    callback.onSuccess("");
                }
            }
            if(CompassAppHelper.INSTANCE.isDebug()) LogUtils.d(TAG, text + ",code=" + i);
        });

    }
    /**
     * 初始化讯飞sdk
     */
//    public static void initIFly(Activity context, IFlyOptCallback callback){
//        initIFly(context,callback);
//    }

    /**
     * 识别文本
     */
    public static void getText(Activity context,IFlyOptCallback callback){
        int delay = 500;
        IText iText = XiriOPManager.getInstance().getTextMgr();
        if(iText!= null){
            LogUtils.d(TAG,  "开始执行 识别语音 active");
            iText.active(textEntity -> {
               LogUtils.d(TAG, "text = " + textEntity.getText() + ", token = " + textEntity.getToken());
               callback.onSuccess(textEntity.getText());
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                IFeedback feedbackMgr = XiriOPManager.getInstance().getFeedbackMgr();
                if (feedbackMgr != null) {
                    feedbackMgr.feedbackUI("Demo已处理，正在处理业务：" + delay,
                            true, true);
                }
               iText.feedbackResult(textEntity.getToken(),true);
           });

        }else {
            initIFly(context,null);
//           callback.onFail(-100,"");
        }
    }

    /**
     * 关闭识别
     */
    public static void cancelText(){
        IText iText= XiriOPManager.getInstance().getTextMgr();
        if(iText!=null){
            iText.invalid();
        }
    }

    public static void getSemantic(Context context,IFlyOptCallback callback){
        ISemantic iSemantic = XiriOPManager.getInstance().getSemanticMgr();
        String mCurrSelcetService = ServiceConstant.SERVICE_JOKE;

    }

    /**
     * 反初始化操作
     */
    public static void unInitIFly(){
        XiriOPManager.getInstance().unInit();
    }

}
